package com.jwmp.rbac.service.impl;

import com.jwmp.core.util.MD5;
import com.jwmp.core.util.MyBatisUtil;
import com.jwmp.core.util.PageResult;
import com.jwmp.core.util.QueryObject;
import com.jwmp.rbac.domain.Employee;
import com.jwmp.rbac.domain.Role;
import com.jwmp.rbac.mapper.EmployeeMapper;
import com.jwmp.rbac.service.IEmployeeService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Version 1.0
 */
public class EmployeeServiceImpl implements IEmployeeService {
    public void insert(Employee emp) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Employee employee = mapper.selectByUsername(emp.getUsername());
        if (employee != null) {
            session.commit();
            session.close();
            throw new RuntimeException("用户名已存在");
        }

        String newPwd = MD5.md5Hasp(emp.getPassword() + emp.getUsername().substring(1));
        emp.setPassword(newPwd);

        mapper.insert(emp);
        session.commit();
        session.close();
    }

    public void update(Employee emp) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Employee employee = mapper.selectByUsername(emp.getUsername());
        if (employee != null) {
            session.commit();
            session.close();
            throw new RuntimeException("用户名已存在");
        }
        // 密码加密
        if (emp.getPassword() != null) {
            String newPwd = MD5.md5Hasp(emp.getPassword() + emp.getUsername().substring(1));
            emp.setPassword(newPwd);
        }

        mapper.update(emp);
        session.commit();
        session.close();
    }

    public void updateStatus(Long id,Boolean status) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        mapper.updateStatus(id,status);
        session.commit();
        session.close();
    }

    public PageResult selectList(QueryObject qo) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        int totalCount = mapper.selectCount(qo);
        if (totalCount <= 0) {
            session.close();
            return new PageResult(qo.getCurrentPage(),qo.getPageSize());
        }
        List<Employee> employeeList = mapper.selectList(qo);
        session.close();
        return new PageResult(qo.getCurrentPage(),qo.getPageSize(),employeeList,totalCount);
    }

    public Employee selectOne(Long id) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Employee employee = mapper.selectOne(id);
        session.close();
        return employee;
    }

    public List<Role> selectRoleNotInEmpId(Long empId) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        List<Role> roleList = mapper.selectRolesByEmpId(false, empId);
        session.close();
        return roleList;
    }

    public List<Role> selectRoleByEmpId(Long empId) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        List<Role> roleList = mapper.selectRolesByEmpId(true, empId);
        session.close();
        return roleList;
    }

    public void bindRole(Long empId, List<Long> roleIdList) {
        List<Role> roleList = selectRoleByEmpId(empId);
        // 1 查询解决下目前存在的角色
        List<Long> existRoleIdList = roleList.stream().map(role -> role.getId()).collect(Collectors.toList());
        // 2 获取新加的角色
        List<Long> newRoleIdList = roleIdList.stream().filter(roleId -> !existRoleIdList.contains(roleId)).collect(Collectors.toList());
        // 3 获取删除的角色
        List<Long> delRoleIdList = existRoleIdList.stream().filter(roleId -> !roleIdList.contains(roleId)).collect(Collectors.toList());
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        if(newRoleIdList.size() > 0){
            mapper.insertBath(empId,newRoleIdList);
        }
        if(delRoleIdList.size() > 0){
            mapper.delBath(empId,delRoleIdList);
        }
        session.commit();
        session.close();
    }

    public Employee login(String username, String password) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        String newPwd = MD5.md5Hasp(password + username.substring(1));

        Employee employee = mapper.login(username,newPwd);
        session.commit();
        session.close();
        return employee;
    }
}
